package org.lex.dbq.view.sql;

import java.sql.Connection;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.lex.dbq.DBQApplication;
import org.lex.dbq.model.jdbc.resultset.SQLResultSet;
import org.lex.dbq.view.sql.result.TableResultViewer;
import org.lex.jdbc.util.ConnectionUtils;
import org.lex.swt.SWTUtils;
import org.lex.swt.app.ActionMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SqlPane extends Composite {
	private Logger log = LoggerFactory.getLogger(SqlPane.class);
	Connection conn = null;

	ObjectPane objectPane = null;

	Text cmd = null;
	private TableResultViewer resultPane = null;

	private SashForm mainSash = null;

	public SqlPane(Composite parent) {
		super(parent, SWT.NONE);
		this.initialize();
	}

	public void setConnection(Connection conn) {
		this.conn = conn;
		this.objectPane.setConnection(conn);
		try {
			String test = ConnectionUtils.testConnection(conn);
			log.info("test:\n" + test);
			cmd.append("select * from test.thread");
		} catch (SQLException e) {
			log.error("", e);
		}
		SWTUtils.setWeight(mainSash, 0.3);
	}

	public Connection getConnection() {
		return this.conn;
	}

	void execute() {
		execute(cmd.getText());
	}

	private void execute(String sql) {
		resultPane.setResultSet(new SQLResultSet(conn, sql));
	}

	private void initialize() {
		// init actions
		ActionMap am = DBQApplication.getApplication().getContext().getActionService().getActionMap(SqlActions.ACTION_MAP_NAME);

		am.put(SqlActions.queryData, new QueryDataAction(this));
		am.put(SqlActions.editTable, new EditTableAction(this));
		// init ui
		this.setLayout(new FillLayout());

		mainSash = new SashForm(this, SWT.HORIZONTAL | SWT.SMOOTH);
		// create object pane
		objectPane = new ObjectPane(mainSash, SWT.LEFT);
		// create sql pane
		this.createSqlPane(mainSash);
	}

	private void createSqlPane(SashForm sash) {
		SashForm sqlSash = new SashForm(sash, SWT.VERTICAL | SWT.RIGHT);
		this.createCommandArea(sqlSash);
		this.createResultArea(sqlSash);
	}

	private void createResultArea(SashForm sash) {
		resultPane = new TableResultViewer(sash, SWT.BOTTOM | SWT.BORDER);
	}

	private void createCommandArea(SashForm sash) {
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 1;
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;

		Composite top = new Composite(sash, SWT.TOP);
		top.setLayout(layout);

		GridData d = null;
		{
			d = new GridData(GridData.FILL_HORIZONTAL);
			d.horizontalSpan = 1;
			d.verticalSpan = 1;

			ToolBar tb = new ToolBar(top, SWT.FLAT);
			tb.setLayoutData(d);

			ToolItem exec = new ToolItem(tb, SWT.PUSH);
			exec.setText("exec");
			exec.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					execute();
				}

			});
		}
		{
			d = new GridData(GridData.FILL_BOTH);
			d.horizontalSpan = 1;
			d.verticalSpan = 1;

			cmd = new Text(top, SWT.MULTI | SWT.BORDER);
			cmd.setLayoutData(d);
			cmd.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.keyCode == SWT.F8) {
						log.debug("F8 pressed");
						execute();
					}
				}
			});
		}
	}
}
